package File;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wuhan
 * @date 2019/10/11 20:04
 */
public class DemoRecursiveFile {
    public static void main(String[] args) {
/*
        File dir=new File("D:\\CXK\\wuhan");
        showDirRecursion(dir);
*/
//        toBin(10);
//        System.out.println(Integer.parseInt(sb.toString()));
//        System.out.println(getSum(100));
        File dir=new File("D:\\Music");

//        File[] files = dir.listFiles();
//        System.out.println(files.length);
        List<File> list =new ArrayList<>();

        StoreFileToList(dir,list);
        writeToFile(list,"a.txt");
        for (File file : list) {
            System.out.println(file.getName());
        }
//        System.out.println(list.size());


    }
    private static int getSum(int num){
        if (num==1)
            return 1;
        return num+getSum(num-1);

    }
    private static void showDirRecursion(File dir){
        File[] files = dir.listFiles();
        System.out.println(dir);
        assert files != null;
        for (File file : files)
            if (file.isDirectory()) {
                showDirRecursion(file);
            } else {
                System.out.println(file);
            }
    }
    private static void toBin(int num){
        if (num>0){
            toBin(num/2);
            sb.append(num%2);
            System.out.println(num%2);
        }
    }
    private static StringBuilder sb=new StringBuilder();


    private static void removeDir(File dir){
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()){
                removeDir(file);
            } else {
                System.out.println(file.toString()+":-file:-"+file.delete());
            }
        }
        System.out.println(dir+"::dir::"+dir.delete());
    }
    public static void StoreFileToList(File file, List<File> list){
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory())
                StoreFileToList(file,list);
            else {
                if (file.getName().endsWith("c")){
                    list.add(file);
                }
            }
        }
    }
    public static void writeToFile(List<File> list,String javaListFile){
        BufferedWriter bufw=null;
        try {
            bufw=new BufferedWriter(new FileWriter(javaListFile));
            for (File file : list) {
                String path=file.getAbsolutePath();
                String fileName=file.toString();
                bufw.write(path);
                bufw.write("  ");
                bufw.write(fileName);
                bufw.newLine();
                bufw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (bufw!=null) {
                try {
                    bufw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
